﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using YSC_Core.BLL.DBContext;
using YSC_Core.BLL.Repository;

namespace YSC_Data.BLL.DBContext
{
    public class EfRepository : IEfRepository
    {
        private IDbContext _context;

        public EfRepository(IDbContext context)
        {
            this._context = context;
        }

        /// <summary>
        /// 获取第一个或默认的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public virtual T FirstOrDefault<T>(Expression<Func<T, bool>> predicate) where T : class, new()
        {
            return _context.Set<T>().FirstOrDefault(predicate);
        }

        public void BulkInsert<T>(IList<T> entities) where T : class, new()
        {
            this._context.BulkInsert(entities);
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        public void Insert<T>(T entity) where T : class, new()
        {
            try
            {
                if (entity == null)
                    throw new ArgumentNullException("entity");

                _context.Set<T>().Add(entity);

                this._context.SaveChanges();
            }
            catch (Exception e)
            {
                
            }
        }

        /// <summary>
        /// 批量插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities"></param>
        public void InsertList<T>(IEnumerable<T> entities) where T : class, new()
        {
            try
            {
                if (entities.Count() == 0)
                    throw new ArgumentNullException("entities");
                _context.Set<T>().AddRange(entities);
                this._context.SaveChanges();
            }
            catch (Exception e)
            {
            
            }
        }


        /// <summary>
        /// 执行SQL获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public virtual IEnumerable<T> SqlQuery<T>(string sql, params object[] parameters) where T : class, new()
        {
            return _context.Set<T>().FromSql(sql, parameters);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        public void Update<T>(T entity) where T : class, new()
        {
            try
            {
                if (entity == null)
                    throw new ArgumentNullException("entity");
                _context.Set<T>().Update(entity);
                this._context.SaveChanges();
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 批量更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities"></param>
        public void UpdateList<T>(IEnumerable<T> entities) where T : class, new()
        {
            try
            {
                if (entities.Count() == 0)
                    throw new ArgumentNullException("entities");
                _context.Set<T>().UpdateRange(entities);
                this._context.SaveChanges();
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// where方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public virtual IQueryable<T> Where<T>(Expression<Func<T, bool>> predicate) where T : class, new()
        {
            return _context.Set<T>().Where(predicate);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        public void Remove<T>(T entity) where T : class, new()
        {
            try
            {
                if (entity == null)
                    throw new ArgumentNullException("entity");
                _context.Set<T>().Remove(entity);
                this._context.SaveChanges();
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities"></param>
        public void RemoveRange<T>(IEnumerable<T> entities) where T : class, new()
        {
            if (entities.Count() == 0)
                throw new ArgumentNullException("entities");
            _context.Set<T>().RemoveRange(entities);
            this._context.SaveChanges();
        }

    }
}
